<?php
require_once( 'Zend/Controller/Action.php');
require_once( 'Zend/Db.php' );
require_once( 'Zend/Db/Table.php' );
require_once( 'Zend/Db/Table/Abstract.php' );
require_once( 'models/Users.php');
require_once( 'modules/default/Form/Form.php');
require_once( 'IndexController.php');
require_once( 'Zend/Exception.php');
require_once( 'InitController.php');
require_once( 'Zend/View.php');

class UsuarioController extends InitController {
	
    public function indexAction()
    {
    	$this->view->title = 'Medisoft - Administración de usuarios';
    	$this->view->message = '<h2> Utilice el menu de la izquierda para agregar, borrar o modificar usuarios por favor. </h2>';  
    }
	
    public function insertAction()
    {
    	$this->view->title = 'Medisoft - Agregar Usuario';  
    	
    	$this->view->identity = $this->_auth->getIdentity();   
    	
    	$this->view->form = $this->getusrsForm();
    }
    
    private function getusrsForm() {
    	$form = new Zend_Form();
        $form->setAction("/index.php/usuario/ins-usuario")->setMethod('post');
            
        
        
        $usrname = $form->createElement('text', 'usuario', array('label' => 'Usuario'));
        $usrname->setRequired(true);
        
        // Create and configure password element:
		$usrpass = $form->createElement('password', 'password', array('label' => 'Clave'));
		$usrpass->addValidator('StringLength', false, array(4))
		         ->setRequired(true);
		         
		// Create and configure password element:
		$confpass = $form->createElement('password', 'confpass', array('label' => 'Repetir clave'));
		$confpass->addValidator('StringLength', false, array(4))
		         ->setRequired(true);
        
        $usrdni = $form->createElement('text', 'dni', array('label' => 'DNI'));
        $usrdni->addValidator('alnum')
		         ->addValidator('regex', false, array('/^[0-9]+/'))
		         ->addValidator('stringLength', false, array(1, 10))
		         ->setRequired(true)
		         ->addFilter('StringToLower');
		         
        $usrpriv = $form->createElement('text', 'privilegios', array('label' => 'Privilegios'));
        $usrpriv->setRequired(true);
        
        $submit = $form->createElement('submit', 'seleccionar', array('label' => 'Seleccionar'));
               
        // Add elements to form:
        $form->addElement($usrname)
   		     ->addElement($usrpass)
   		     ->addElement($confpass)
             ->addElement($usrdni)
             ->addElement($usrpriv)
             ->addElement($submit)
             ->addDisplayGroup(array('usuario', 'password','confpass','dni','privilegios','seleccionar'), 'user', array("legend" => "Info Usuario"));
        $form->setAttrib('onsubmit',"return confirm('¿Está seguro que quiere Ingresar los datos del Usuario?')");
        	  
        
        return $form;
    }
    
public function insUsuarioAction() {
    	$this->view->title = 'Medisoft - Agregar Usuario';
    	$request = $this->getRequest();

    	// VALIDAR?
    	
        // Check if we have a POST request
        if ($request->isPost()) {
        	
        // Get our form and validate it
        	
        	$form = $this->getusrsForm();
        	if (!$form->isValid($request->getPost())) {
        	    // Invalid entries
        	    $this->view->form = $form;
        	    
        	    return $this->render('insert'); // re-render the form
        	}
    		$conf = $_POST['confpass'];
        	$table = new MED_USER();
        	
        	$table->usrname = $_POST['usuario'];
        	$table->clave = $_POST['password'];
        	$table->dni = $_POST['dni'];
        	$table->privilegios = (int)$_POST['privilegios'];
        	if ($table->clave == $conf)
        	{
        		$state = $table->insUsuarios();
        		if ( $state == 1 ){
					$this->view->message = "El Usuario ha sido agregado con éxito";
				}
				else 
					$this->view->message = "No se pudo agregar el Usuario";
					
        	}
        	else 
        		$this->view->message = "Las claves no coinciden";
        }
        else
        	$this->_forward('insert');
    }
}